package com.caishi.lkx.exam.model;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.caishi.lkx.common.model.Seo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zzw.common.entity.impl.BaseIntEntity;
import com.zzw.common.service.model.NameModel;
import com.zzw.common.service.model.base.BaseTreeModel;
import com.zzw.common.service.model.base.SortModel;
import com.caishi.lkx.exam.records.dto.RecordsCountRecordDto;
import com.caishi.lkx.order.IAssetsModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.util.List;

@Schema(title = "科目")
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "ex_category")
public class CategoryModel extends BaseIntEntity<CategoryModel> implements
        NameModel<CategoryModel, Long>,
        BaseTreeModel<CategoryModel, Long>,
        SortModel<CategoryModel>,
        IAssetsModel<CategoryModel, Long>,
        CstkModel, Seo {

    /**
     * 科目名称
     */
    private String name;

    /**
     * 后台显示名称
     */
    private String serverName;

    /**
     * 上级科目
     */
    private Long parentId;

    /**
     * 所属考试id
     */
    private String examId;

    /**
     * 行业id
     */
    private String industryId;

    private Integer sort;

    /**
     * 6.0对应的科目id集合
     */
    @TableField(value = "vers_ids", typeHandler = JacksonTypeHandler.class)
    private List<Integer> versIds;

    /**
     * 是否展示在前台，默认不展示在前台 0：不展示在前台，1展示在前台
     */
    private Integer showState;

    /**
     * 属性
     * 0x01 目前最后一级科目
     * 0x02 热门科目
     * 0x04 推荐科目
     * 0x08 最新科目
     * 0x16 基础科目
     */
    private Integer attribute;

    @JsonIgnore
    private String cstkId;

    @TableField(exist = false)
    private String industryName;

    @TableField(exist = false)
    private List<ColumnModel> columns;

    @TableField(exist = false)
    private String examName;

    @TableField(exist = false)
    private CategoryModel parent;

    @TableField(exist = false)
    private List<CategoryModel> children;

    @TableField(exist = false)
    private boolean haveColumn;
    //科目下用户记录
    @TableField(exist = false)
    private RecordsCountRecordDto categoryRecords;

    @Schema(title = "是否还有子科目")
    public boolean getHaveChild() {
        return attribute != null && (attribute & 1) == 0;
    }

    @Override
    public String getCode() {
        return getId() == null ? null : getId().toString();
    }

    @Override
    public String getKeyword() {
        return getName();
    }

    @Override
    public Long getPrice() {
        return 0L;
    }


    @TableField(exist = false)
    private byte authFlag;

    @TableField(exist = false)
    private boolean selected = false;

    public String getServerName() {
        return StrUtil.isEmpty(serverName) ? name : serverName;
    }
}
